package com.smartfactory.sail.controller;


import com.ada.base.common.ApiResponse;
import com.ada.base.common.ControllerResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.smartfactory.sail.config.HeaderWrapper;
import com.smartfactory.sail.constant.header.OrderItemEnum;
import com.smartfactory.sail.entity.Item;
import com.smartfactory.sail.entity.User;
import com.smartfactory.sail.entity.enums.ItemStatusEnum;
import com.smartfactory.sail.entity.enums.UserStatusEnum;
import com.smartfactory.sail.service.IItemService;
import com.smartfactory.sail.service.IOrderItemService;
import com.smartfactory.sail.service.IUserService;
import com.smartfactory.sail.vo.PageParam;
import com.smartfactory.sail.vo.order.ListOrderItemRsp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 订单产品表 前端控制器
 * </p>
 *
 * @author 王巨壁
 * @since 2019-08-13
 */
@Api(tags = "订单产品")
@RestController
@RequestMapping("/order-item")
public class OrderItemController extends ControllerResponse {
    private IOrderItemService iOrderItemService;
    private IUserService iUserService;
    private IItemService iItemService;

    public OrderItemController(IOrderItemService iOrderItemService, IUserService iUserService, IItemService iItemService) {
        this.iOrderItemService = iOrderItemService;
        this.iUserService = iUserService;
        this.iItemService = iItemService;
    }

    @HeaderWrapper(OrderItemEnum.class)
    @ApiOperation(value = "订单产品列表（压铸 五金 组装）", notes = "订单产品列表（压铸 五金 组装）")
    @PostMapping("OrderItemList/{currentPage}/{size}/{sortType}/{sortField}")
    public ApiResponse<IPage<ListOrderItemRsp>> contractOrderList(@RequestBody Map<String, Object> queryParam,
                                                                  @PathVariable("currentPage") Long currentPage,
                                                                  @PathVariable("size") Integer size,
                                                                  @PathVariable("sortType") Integer sortType,
                                                                  @PathVariable("sortField") String sortField) {

        PageParam pageParam = new PageParam(currentPage, size, sortType, "a." + sortField);
        return ok(iOrderItemService.getOrderItemPage(pageParam, queryParam));
    }

    @ApiOperation(value = "扫码获取内容", notes = "扫码获取内容1员工2产品")
    @GetMapping("sweepCode/{qrCode}/{type}")
    public ApiResponse<Map<String, Object>> contractOrderList(@PathVariable("qrCode") String qrCode, @PathVariable("type") Integer type) {
        HashMap<String, Object> map = new HashMap<>();
        if (type == 1) {//用户
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.eq("staffNo", qrCode);
            int count = iUserService.count(userQueryWrapper);
            if (count == 1) {
                User user = iUserService.list(userQueryWrapper).get(0);
                if (UserStatusEnum.ZZ != user.getStatus()) {
                    return fail("该员工已经 " + user.getStatus().getValue());
                }
                map.put("userName", user.getUsername());
                map.put("userId", user.getId());
                map.put("code", qrCode);
                return ok(map);
            } else if (0 == count) {
                return fail("该员工不存在!");
            } else {
                return fail("数据异常!");
            }
        } else if (2 == type) {
            QueryWrapper<Item> itemQueryWrapper = new QueryWrapper<>();
            itemQueryWrapper.eq("code", qrCode);
            int count = iItemService.count(itemQueryWrapper);
            if (1 == count) {
                Item item = iItemService.list(itemQueryWrapper).get(0);
                if (ItemStatusEnum.TC == item.getStatus()) {
                    return fail("该产品已停产!");
                }
                map.put("userName", item.getName());
                map.put("userId", item.getId());
                map.put("code", qrCode);
                return ok(map);
            } else if (0 == count) {
                return fail("该产品不存在");
            } else {
                return fail("数据异常!");
            }
        } else {
            return fail("请检查接口参数!");
        }
    }
}
